'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceChoosemultinfoCtrl
 * @description
 * # FinanceChoosemultinfoCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('FinanceChoosemultinfoCtrl', function ($scope, Util, openApi, localStorageService, PagingService) {
    $scope.multiple = true;
    var relModuleObj;
    var param = {};
    // 获取搜索所需参数以及标题信息
    relModuleObj = Util.getDataforsearch();
    param.relatedModule = relModuleObj.selectTab;
    param = angular.extend(param, relModuleObj.param || {});
    $scope.title=relModuleObj.selectTabContent;
    // 选择后回传的数据
    $scope.choosePeople = function (index) {
      var item = $scope.taskList.items[index]
      item.selected = !item.selected;
      // $scope.submit(index);
      $scope.selectLists=getSelectedData();
    };
    var getPropertyValue = function () {
      var selectedData = $scope.selectLists;
      var data = [];
      selectedData.forEach(function (item, index) {
        data.push(item[relModuleObj.propertyValue])
      })
      return data.toString();
    }
    var getSelectedData = function () {
      var selectedData = $scope.taskList.items.filter(function (item, index) {
        return item.selected;
      });
      return selectedData
    }
    $scope.submit = function () {
      if ($scope.selectLists.length<1) {
        Util.Toast("请选择"+relModuleObj.selectTabContent);
        return
      }
      // 如果是在picker上面则直接挂在picker上面
      var pickerdata=localStorageService.get("pickerdata");
      if(pickerdata){
          pickerdata[relModuleObj.propertyName]= getPropertyValue()
          localStorageService.set("pickerdata",pickerdata);
        }else{
          relModuleObj.filterData = {};
          relModuleObj.filterData[relModuleObj.propertyName] = getPropertyValue();
          Util.savaDataforsearch(relModuleObj);
        }
      Util.goBack();

    }
    $scope.filterSql = param.filterSql;
    $scope.search = function () {
      param.filterSql = $scope.filterSql;
      $scope.taskList = new PagingService(openApi.getRelpage, param);
    }
    $scope.search();
  });